﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;

namespace SUIE
{
    [ToolboxItemAttribute(false)]
    public class Accordion : WebPart
    {
        // A Div to display the Content of the Tab.
        private HtmlGenericControl _divTabContent;

        protected override void CreateChildControls()
        {
            this.Hidden = true;
            this.ChromeType = PartChromeType.Default;
            this.CssClass = "SUIEAccordion SUIEAccordion-" + this.ID;

            this._divTabContent = new HtmlGenericControl("div");
            this._divTabContent.ID = "SUIEAccordionContent";
            this._divTabContent.InnerHtml = "<a href=\"javascript:ShowToolPane2Wrapper('Edit',this,'" + this.ID + "')\">Edit</a> this Web Part to configure the Accordion.";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIEAccordionEnabled'>" + this.AccordionEnabled.ToString() + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIEAccordionFirstItemOpened'>" + this.AccordionFirstItemOpened.ToString() + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIEAccordionAutoHeight'>" + this.AccordionAutoHeight.ToString() + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIEAccordionClearStyle'>" + this.AccordionClearStyle.ToString() + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIEAccordionCollapsible'>" + this.AccordionCollapsible.ToString() + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIEAccordionIcon'>" + this.AccordionIcon + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIEAccordionEvent'>" + this.AccordionEvent + "</div>";
            this._divTabContent.InnerHtml += "<div class='SUIEHidden SUIEAccordionTheme'>" + this.AccordionTheme + "</div>";
            this._divTabContent.InnerHtml += "<script type=\"text/javascript\">$(document).ready(function () { $('.SUIEAccordion-" + this.ID + "').Accordion(); }); </script>";

            this.Controls.Add(_divTabContent);
        }
        protected override void OnPreRender(EventArgs e)
        {
            // add css            
            AddCss("SUIEjQueryUI", "/_layouts/SUIE/css/jquery-ui/smoothness/jquery-ui-1.8.16.custom.css");
            AddCss("SUIEAccordionTheme-" + this.ID, "/_layouts/SUIE/css/jquery-ui/" + this.AccordionTheme + "/jquery-ui-1.8.16.custom.css");
            AddCss("SUIEAccordionBaseStyles", "/_layouts/SUIE/css/Accordion.css");

            const string jquery = "jquery-1.6.2.min.js";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), jquery))
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), jquery, " <script type=\"text/javascript\" src=\"/_layouts/SUIE/scripts/jquery-1.6.2.min.js\" ></script>");
            }
            const string jqueryUI = "jquery-ui-1.8.16.custom.min.js";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), jqueryUI))
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), jqueryUI, " <script type=\"text/javascript\" src=\"/_layouts/SUIE/scripts/jquery-ui-1.8.16.custom.min.js\" ></script>");
            const string SUIETabber = "Accordion.js";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), SUIETabber))
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), SUIETabber, " <script type=\"text/javascript\" src=\"/_layouts/SUIE/scripts/Accordion.js\" ></script>");
            base.OnPreRender(e);
        }

        private void AddCss(string id, string href)
        {
            HtmlHead head = (HtmlHead)Page.Header;
            HtmlLink link = new HtmlLink();
            link.Attributes.Add("id", id);
            link.Attributes.Add("href", href);
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            bool styleExists = false;
            foreach (Control c in head.Controls)
            {
                if (c is HtmlLink)
                {
                    HtmlLink csslink = c as HtmlLink;
                    if (csslink.Href == href)
                    {
                        styleExists = true;
                        break;
                    }
                }
            }
            if (!styleExists)
                head.Controls.Add(link);
        }

        public void SaveChanges()
        {
            // This method sets a flag indicating that the personalization data has changed.
            // This will allow the changes to the Web Part properties from outside the Web Part class.
            this.SetPersonalizationDirty();
        }

        // properties       
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue(true)]
        public Boolean AccordionEnabled
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue(true)]
        public Boolean AccordionFirstItemOpened
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue(true)]
        public Boolean AccordionAutoHeight
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue(true)]
        public Boolean AccordionClearStyle
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue(true)]
        public Boolean AccordionCollapsible
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue("arrow")]
        public String AccordionIcon
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue("click")]
        public String AccordionEvent
        {
            get;
            set;
        }
        [WebBrowsable(false)]
        [DefaultValue("ui-lightness")]
        [Personalizable(PersonalizationScope.Shared)]
        public String AccordionTheme
        {
            get;
            set;
        }

        public override object WebBrowsableObject
        {
            // Return a reference to the Web Part instance.
            get { return this; }
        }

        public override EditorPartCollection CreateEditorParts()
        {
            AccordionConfigurationEditorPart editorPart = new AccordionConfigurationEditorPart();

            // The ID of the Editor part should be unique. So prefix it with the ID of the Web Part.
            editorPart.ID = this.ID + "_SUIEAccordionConfigurationEditorPart";

            // Create a collection of Editor Parts and add them to the Editor Part collection.
            List<AccordionConfigurationEditorPart> editors = new List<AccordionConfigurationEditorPart> { editorPart };
            return new EditorPartCollection(editors);
        }
    }
}
